package moonlit.chill.pay.service.impl.ali;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import lombok.extern.slf4j.Slf4j;
import moonlit.chill.pay.cache.PayConfigDataCache;
import moonlit.chill.pay.constants.Constants;
import moonlit.chill.pay.service.PayStrategy;
import moonlit.chill.pay.util.AliPayClientUtil;
import moonlit.chill.pay.vo.PayConfig;
import moonlit.chill.pay.vo.TradeParam;
import moonlit.chill.pay.vo.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author MoonlitChill
 * @date 2024/4/15 17:00
 */
@Slf4j
@Service("aliPcPayStrategy")
public class AliPcPayStrategyServiceImpl implements PayStrategy {
    @Resource
    private PayConfigDataCache payConfigDataCache;

    @Autowired
    private AliPayClientUtil aliPayClientUtil;

    private static final String ALI_PC_PAY_CODE = "FAST_INSTANT_TRADE_PAY";

    @Override
    public PayResult<?> payMethod(TradeParam tradeParam) {
        log.info(String.format("支付宝PC支付参数:%s", JSONUtil.toJsonStr(tradeParam)));
        PayConfig payConfig = payConfigDataCache.getPayConfig();
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        PayResult<String> result = new PayResult<>();
        aliPayRequest.setReturnUrl(payConfig.getReturnUrl());
        aliPayRequest.setNotifyUrl(payConfig.getNotifyUrl());
        String outTradeNo = tradeParam.getTransNum();
        result.setTransNum(outTradeNo);
        model.setTotalAmount(tradeParam.getTotalFee().toString());
        model.setSubject(tradeParam.getPayTitle());
        model.setProductCode(ALI_PC_PAY_CODE);
        model.setBody(JSONUtil.toJsonStr(tradeParam.getAdditionalInfo()));
        model.setOutTradeNo(outTradeNo);
        model.setTimeExpire(DateUtil.format(tradeParam.getPayDeadLine(), DatePattern.NORM_DATETIME_PATTERN));
        aliPayRequest.setBizModel(model);
        AlipayTradePagePayResponse payResponse;
        try {
            AlipayClient aliPayClient = aliPayClientUtil.getAliPayClient(payConfig);
            payResponse = aliPayClient.pageExecute(aliPayRequest, "GET");
            log.info(String.format("支付宝PC支付参数:%s  返回数据:%s", JSONUtil.toJsonStr(tradeParam), JSONUtil.toJsonStr(payResponse)));
            if (!payResponse.isSuccess()) {
                result.error(payResponse.getSubMsg(), Constants.PAY_FAIL_CODE);
            } else {
                result.setSuccess(Boolean.TRUE);
                result.setResult(payResponse.getBody());
            }
        } catch (AlipayApiException e) {
            result.errorWithException(e.getErrMsg(), Constants.PAY_ERROR_CODE, e);
        }
        return result;
    }
}
